package com.taichuan.selfcheck.ui.adapter;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.taichuan.keysupport.Key;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.entity.file.FileInfo;
import com.taichuan.tcutils.taichuan.FocusUtil;
import com.taichuan.tcutils.taichuan.SystemUtils;


/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2020/7/27
 */
public class FileListAdapter extends BaseQuickAdapter<FileInfo, BaseViewHolder> {

    private OnItemClickListener mListener;

    public FileListAdapter(OnItemClickListener listener) {
        super(R.layout.item_file_list);
        this.mListener = listener;
    }

    @Override
    protected void convert(BaseViewHolder helper, FileInfo item) {
        helper.setText(R.id.file_list_item_file_name_textView, item.getFileName());
        helper.setText(R.id.file_list_item_file_size_textView, item.getFileSize());
        helper.setText(R.id.file_list_item_file_permission_textView, item.getFilePermission());
        helper.setText(R.id.file_list_item_file_date_textView, item.getFileDate());
        helper.setImageResource(R.id.file_list_item_file_imageView,
                item.isDirectory() ? R.mipmap.ic_folder : R.mipmap.icon_file_unknown);

        ConstraintLayout layout = helper.itemView.findViewById(R.id.file_list_item_layout);

        if (!SystemUtils.isTouchDevice()) {
            layout.setFocusable(true);
            layout.setFocusableInTouchMode(true);
            helper.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    if (hasFocus) {
                        FocusUtil.onFocusIn(view, 1.02f);
                    } else {
                        FocusUtil.onFocusOut(view, 1.02f);
                    }
                }
            });
            if (null != mData && mData.size() > 0) {
                if (helper.getAdapterPosition() == 0) {
                    helper.itemView.requestFocus();
                }
            }

            //为按键版设备的RecyclerView设置item点击事件
            helper.itemView.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    Log.i(TAG, "onKey :" + keyCode);
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        switch (keyCode) {
                            case Key.Convert.KEYCODE_JING://选中item
                                if (null != mListener) {
                                    mListener.onItemClick(helper.getAdapterPosition(), item);
                                }
                                break;
                        }
                    }
                    return false;
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, FileInfo item);
    }
}
